<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="78px">
      <!-- <el-form-item label="负责人电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入负责人电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="负责人" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入负责人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="商家名称" prop="storeName">
        <el-input v-model="queryParams.storeName" placeholder="请输入产品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:join:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:join:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:join:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:join:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="joinList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="用户类型" align="center" prop="roleType" /> -->
      <el-table-column label="供应商信息" align="center" prop="storeName" width="300">
        <template slot-scope="{row}">
          <div class="table-user">
            <div class="table-user-img">
              <image-preview :src="row.img" :width="50" :height="50" />
            </div>
            <div>
              <div class="table-user-name">名称：{{ row.storeName }}</div>
              <div class="table-user-phone">详细地址：{{ row.address }}</div>
            </div>
          </div>
        </template>
      </el-table-column>

      <!-- <el-table-column label="产品名称" align="center" prop="productName" /> -->
      <el-table-column label="负责人" align="center" prop="name" />
      <el-table-column label="负责人电话" align="center" prop="phone" />
      <el-table-column label="营业执照" align="center" prop="licenseImg" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.licenseImg" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 1">审核通过</span>
          <span v-if="scope.row.status == 2">审核不通过</span>
          <span v-if="scope.row.status == 0">待审核</span>
        </template>
      </el-table-column>
      <el-table-column label="注册时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <el-button size="mini" type="text" v-if="scope.row.status == 0" @click="audit(scope.row, 1)"
            v-hasPermi="['system:join:audit']">审核通过</el-button>


          <el-button size="mini" type="text" v-if="scope.row.status == 0" @click="audit(scope.row, 2)"
            v-hasPermi="['system:join:audit']">审核不通过</el-button>

          <!-- <el-button
            size="mini"
            type="text"
              v-if="scope.row.isAbleUse !=1"
            @click="setAbleUse(scope.row,1)"
            v-hasPermi="['system:join:edit']"
          >启用</el-button>

          <el-button
            size="mini"
            type="text"
              v-if="scope.row.isAbleUse !=2" 
            @click="setAbleUse(scope.row,2)"
            v-hasPermi="['system:join:edit']"
          >禁用</el-button> -->
          <!-- 
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:join:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:join:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改加盟对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="商家名称" prop="storeName">
          <el-input v-model="form.storeName" placeholder="请输入商家名称" />
        </el-form-item>
        <el-form-item label="负责人" prop="name">
          <el-input v-model="form.name" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="负责人电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入负责人电话" />
        </el-form-item>
        <!-- <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item> -->
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <!-- <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listJoin, getJoin, delJoin, addJoin, updateJoin, updateFxToJoinWithEasy } from "@/api/fx/join";

export default {
  name: "Join",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 加盟表格数据
      joinList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roleType: 3,
        phone: null,
        name: null,
        storeName: null,
        productName: null,
        address: null,
        saleType: null,
        status: 0,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询加盟列表 */
    getList() {
      this.loading = true;
      listJoin(this.queryParams).then(response => {
        this.joinList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        roleType: 3,
        phone: null,
        name: null,
        storeName: null,
        productName: null,
        address: null,
        saleType: null,
        remark: null,
        status: 0,
        delFlag: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加加盟";
    },

    audit(row, auditState) {
      this.resetForm("queryForm");
      this.form.id = row.id;
      this.form.status = auditState;
      updateFxToJoinWithEasy(this.form).then(response => {
        this.$modal.msgSuccess("已审核");
        this.open = false;
        this.getList();
      });
    },

    setAbleUse(row, ableState) {
      this.resetForm("queryForm");
      this.form.id = row.id;
      this.form.isAbleUse = ableState;
      updateJoin(this.form).then(response => {
        this.$modal.msgSuccess("操作成功");
        this.open = false;
        this.getList();
      });
    },


    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getJoin(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改加盟";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateJoin(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addJoin(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除加盟编号为"' + ids + '"的数据项？').then(function () {
        return delJoin(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/join/export', {
        ...this.queryParams
      }, `join_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lange="sass" scoped>
.table-user {
  width: 100%;
  height: 100%;
  display: flex;
  /* justify-content: center; */
  align-items: center;
  text-align: left !important;

  .table-user-img {
    width: 3vw;
    height: 6vh;
    margin-right: 10px;
    overflow: hidden;

    img {
      width: 100%;
      height: 100%;
      /* border-radius: 50%; */
      object-fit: cover;
    }
  }

}
</style>